﻿using System;
using System.Collections.Generic;
using System.Windows;
using ComicQ.Controller;
using ComicQ.Domain.Table;

namespace ComicQ.Dektop.View
{
    /// <summary>
    /// Interaction logic for TransactionView.xaml
    /// </summary>
    public partial class OrderView
    {
        private readonly OrderController _orderControl;
        private readonly OrderStatusController _orderStatusControl;
        private readonly OrderDetailController _orderDetailControl;
        private readonly StaffController _staffControl;

        private IList<Order> _orders;
        private IList<OrderData> _orderDatas;
        private readonly IList<OrderStatus> _orderStatuses;
        private readonly Staff _staff;

        public OrderView(Staff staff)
        {
            InitializeComponent();

            _orderControl = new OrderController();
            _orderStatusControl = new OrderStatusController();
            _orderDetailControl = new OrderDetailController();
            _staffControl = new StaffController();

            _orders = _orderControl.GetAllOrder();
            _orderDatas = _orderControl.GetAllOrderData();
            dataGridOrder.ItemsSource = _orderDatas;

            _orderStatuses = _orderStatusControl.GetAllOrderStatus();
            _staff = staff;
        }

        private void RefreshGrid()
        {
            dataGridOrder.ItemsSource = null;
            _orderDatas = _orderControl.GetAllOrderData();
            _orders = _orderControl.GetAllOrder();
            dataGridOrder.ItemsSource = _orderDatas;
        }

        private void ButtonAddClick(object sender, RoutedEventArgs e)
        {
            var newWindow = new OrderEditorView(_staff);

            if (newWindow.ShowDialog() != false)
            {
            }
            else
            {
                RefreshGrid();
            }
        }

        private void ButtonEditClick(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dataGridOrder.SelectedItem == null)
                    throw new NullReferenceException();

                var order = _orders[dataGridOrder.SelectedIndex];

                var newWindow = new ReturnView(order);

                if (newWindow.ShowDialog() != false)
                {
                }
                else
                {
                    var newOrder = new Order
                    {
                        Id = order.Id,
                        Member = order.Member,
                        Staff = order.Staff,
                        RentDate = order.RentDate,
                        ReturnDate = order.ReturnDate,
                        Warranty = order.Warranty,
                        WarrantyNumber = order.WarrantyNumber,
                        Total = order.Total
                    };

                    var check = _orderDetailControl.CheckOrder(order);

                    switch (check)
                    {
                        case 0:
                            newOrder.OrderStatus = _orderStatuses[1];
                            break;
                        case 1:
                            newOrder.OrderStatus = _orderStatuses[2];
                            break;
                        default:
                            newOrder.OrderStatus = _orderStatuses[0];
                            break;
                    }

                    _orderControl.EditOrder(newOrder);
                    RefreshGrid();
                }
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("pilih salah satu order");
            }
        }
    }
}
